package com.cangli.controller;

import com.cangli.model.Book;
import com.cangli.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller //控制器
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @RequestMapping("/list")
    public String list(Model model) {
        model.addAttribute("books",bookService.getBooks());
        return "list";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addForm() {
        return "add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addBook(@RequestParam String bookName, @RequestParam String bookAuthor) {
        bookService.addBook(bookName, bookAuthor);
        return "redirect:/";
    }

    @RequestMapping("/remove")
    public String removeBook(@RequestParam int id) {
        bookService.removeBook(id);
        return "redirect:/";
    }


}
